package br.jus.tjpe.bean;

import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import br.jus.tjpe.dominio.Vara;
import br.jus.tjpe.negocio.VaraServico;
import br.jus.tjpe.util.MessageResource;
import br.jus.tjpe.util.MessageResourceFactory;

public class VaraBean implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	
	private List<Vara> varas;

	private Vara vara;

	@EJB
	private VaraServico varaServico;
	
	public List<Vara> getVaras() {
		if (varas == null) {
			carregarVaras();
		}
		return varas;
	}

	public void setVaras(List<Vara> varas) {
		this.varas = varas;
	}

	public Vara getVara() {
		return vara;
	}

	public void setVara(Vara vara) {
		this.vara = vara;
	}
	
	public void carregarVaras() {
		this.varas = this.varaServico.consultarVaras();
	}

	public String manterVara() {
		carregarVaras();
		
		return "manterVara";
	}

	public String cadastrarVara() {
		this.vara = new Vara();

		return "cadastrarVara";
	}

	public String removerVara() {
		this.varaServico.remover(this.vara);
		carregarVaras();
		return "manterVara";
	}

	public String editarVara() {
		return "cadastrarVara";
	}

	public String salvar() {
		String outcome = null;
		boolean valido = true;

		FacesContext context = FacesContext.getCurrentInstance();
		MessageResource message = MessageResourceFactory.createMessageResource("mensagens");

		if (vara.getNome() == null || vara.getNome().isEmpty()) {
			context.addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_WARN, message.get("vara.nome.obrigatorio"), message
							.get("vara.nome.obrigatorio")));
			valido = false;
		}

		if (valido) {
			this.varaServico.salvar(vara);
			outcome = "manterVara";
			carregarVaras();
		} else {
			outcome = "cadastrarVara";
		}

		return outcome;
	}

}
